#include <stdio.h>
#include "atmi.h"		/* TUXEDO  Header File */
#define BUF_SIZE  1024*5
#define MSG_SIZE  4000
#define MSG_SIZE3 4121
#define HDR_SIZE  120

FILE *f_m1, *f_m2;
FILE *f_m3, *f_m4;

FILE *f_i2, *f_i4;	

call_svc(char *svc_name, char *s_buf, char *r_buf, int s_len, int *r_len)
{
	char *sendbuf, *rcvbuf;
	long sendlen, rcvlen;

	if((sendbuf = (char *) tpalloc("CARRAY", NULL, BUF_SIZE)) == NULL) {
		(void) fprintf(stdout,"Error allocating send buffer\n");
		tpterm();
		exit(1);
	}

	if((rcvbuf = (char *) tpalloc("CARRAY", NULL, BUF_SIZE)) == NULL) {
		(void) fprintf(stdout,"Error allocating receive buffer\n");
		tpfree(sendbuf);
		tpterm();
		exit(1);
	}
	memset( sendbuf, 0, BUF_SIZE);
	memset( rcvbuf, 0, BUF_SIZE);

	memcpy(sendbuf, s_buf, s_len);
	sendlen = s_len;
#if 0  
	fprintf( stdout, "---- svc call buffer ----------------------------\n");
	HexLog( sendbuf, s_len );
#endif

	if((tpcall(svc_name, (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0)) == -1) {
		(void) fprintf(stdout, "Can't send request to service(%s)\n",tpstrerror(tperrno));
		tpfree(sendbuf);
		tpfree(rcvbuf);
		tpterm();
		exit(1);
	}
#if 0
printf(" ---- SVC(%s) Return >len%d<\n", svc_name, rcvlen);
#endif
	*r_len = rcvlen;

#if 0
	fprintf( stdout, "---- svc call return ----------------------------\n");
	HexLog( rcvbuf, rcvlen );
#endif

	memcpy(r_buf, rcvbuf, rcvlen);
	tpfree(sendbuf);
	tpfree(rcvbuf);

	return(0);	
#if 0
	sprintf(in_file_name, "data/%s.txt", svc_name);
	sprintf(out_file_name, "data/%s.out", svc_name);
printf(">%s<\n", in_file_name);
	/* ------------------------------------------------------------------ */

	fp_in = fopen( in_file_name, "rb" );
	fp_out = fopen( out_file_name, "wb" );

	if(fp_in == NULL){
		fprintf(stdout,"input file open err\n");
		exit(0);
	}
	if(fp_out == NULL){
		fprintf(stdout,"output file open err\n");
		exit(0);
	}


	read_size = fread((char *)tmp_str, 1, (int)1024, (FILE *)fp_in);
	


	fprintf( stdout, ">SVC_NAME: %s<\n", svc_name);
	/* ------------------------------------------------------------------ */
	fprintf( stdout, ">%s(%d)<\n", tmp_str, read_size);
	/*
	HexLog( tmp_str, read_size);
*/

	memset( sendbuf, 0x00, 8);
	strcpy( sendbuf,  svc_name);
	Ascii2EbcdicStr( tmp_str, sendbuf + 8, read_size - 1);

	sendlen = 8 + read_size - 1;
	HexLog( sendbuf, sendlen);


	/* ------------------------------------------------------------------ */


	fprintf( stdout, ">rcvlen: %d<\n", rcvlen);
	HexLog( rcvbuf, rcvlen);

	Ebcdic2AsciiStr( rcvbuf, tmp_str, rcvlen);
	printf("rcv[%d]:%s\n", rcvlen, tmp_str);

	fwrite((char *)rcvbuf, 1, rcvlen, (FILE *)fp_out);

	if(fp_in) fclose(fp_in);
	if(fp_out) fclose(fp_out);

#endif
}

mk_msg1(char *i1str, char *m1str, int *i1str_len, int m1str_len)
{
	m1str_len -= 2;

	memcpy( i1str, m1str, m1str_len );

	*i1str_len = m1str_len + 1;
	i1str [m1str_len] = 0x1c;
#if 0
	HexLog( m1str, m1str_len );
	HexLog( i1str, *i1str_len );
#endif

}

mk_msg2(char *istr, char *mstr, int *istr_len, int mstr_len)
{
	char hdr_buff	[ HDR_SIZE + 1 ] ;
	char mstr_buff	[ BUF_SIZE ] ;
	int msg_sz;
	char msg_length [4 + 1];

	msg_sz = mstr_len + HDR_SIZE + 1;
#if 0
fprintf( stderr, "---- mk_msg2.mstr_len:%d\n", mstr_len);
fprintf( stderr, "---- mk_msg2.msg_sz:%d\n", msg_sz);

	fprintf( stdout, "---- src. hdr(EBCDIC)\n");
	HexLog( mstr, HDR_SIZE );
#endif

	memset( mstr_buff, 0, BUF_SIZE);
	memcpy( mstr_buff, mstr, HDR_SIZE);
	Ebcdic2AsciiStr( mstr_buff, hdr_buff, HDR_SIZE);
#if 0
	fprintf( stdout, "---- dst. hdr(ASC II)\n");
	HexLog( hdr_buff, HDR_SIZE );
#endif
	memcpy( istr, hdr_buff, HDR_SIZE );

#if 0
	fprintf( stdout, "---- dst. buffer(ASC II)_step01\n");
	HexLog( istr, msg_sz );
#endif
	memcpy( istr + HDR_SIZE, mstr, mstr_len );
#if 0
	fprintf( stdout, "---- dst. buffer(ASC II)_step02\n");
	HexLog( istr, msg_sz );
#endif
#if 0
fprintf( stderr, "length : %c %c %c %c\n", 
*(istr+42),
*(istr+43),
*(istr+44),
*(istr+45)
);
#endif
	sprintf( msg_length, "%c%c%c%c", 
		*(istr+42),
		*(istr+43),
		*(istr+44),
		*(istr+45)
	);
	*istr_len = atoi( msg_length) + HDR_SIZE * 2 + 1;

	*(istr + (*istr_len ) -1) = 0x1c;
#if 0
fprintf( stderr," length : %0d\n", *istr_len);
#endif
#if 0
	fprintf( stdout, "---- dst. buffer(ASC II)_step03\n");
	HexLog( istr, msg_sz );
#endif

}
int HexChk(char *a_data, char *b_data, int len)
{
	int i;
	for( i = 0; i < len; i++ ){
		if( *(a_data + i) != *(b_data + i) ){
			return(1);
		}
	}
	return(0);
}
chk_msg( char *mstr, char *istr, int msg_len)
{
	int i;
	int ret;

#if 1
	ret = HexChk( mstr, istr, msg_len );
	fprintf(stderr, "---------------- HexChk ret:%d\n", ret);
	if (ret) {
		fprintf(stdout, " ----------------------------------------------------------------> Not! same.\n");
		ret = HexChkPr( mstr, istr, msg_len );
		fprintf( stdout, "> medit\n");
		HexLog( mstr, msg_len );
		fprintf( stdout, "> iio\n");
		HexLog( istr, msg_len );
	} else {
		fprintf(stdout, " ----------------------------------------------------------------> OK! same.\n");
	}
#endif
#if 0
	for ( i = 0; i < msg_len; i++ ){
fprintf( stdout, "mstr[i]:%d .vs. istr[i]:%d\n", *(mstr + i), *(istr + i));
		if( mstr[i] != istr[i] ) {
			printf("X ");
		} else {
			printf("O ");
		}
	}
#endif
}

file_write(char *file_name, char *cnt, int sz)
{
	char file_path [1024];
	FILE	*f_mm;

	sprintf( file_path, "../../data/iio_test/rslt/%s", file_name);

	if((f_mm = fopen( file_path, "wb" )) == NULL){
		fprintf(stderr,"%s file open err\n", file_path);
		exit(0);
	}
	if( fwrite(cnt, sz, 1, f_mm) != 1)
		fprintf(stderr, "%s file write err\n", file_path);
	fclose(f_mm);
}

main(int argc, char *argv[])
{
	int ret;
	FILE *fp_in, *fp_out;
	char in_file_name [1024];
	char out_file_name [1024];
	char svc_name [1024];
	char tgt_name [1024];
	char write_file_name [1024];

	char tmp_str [BUF_SIZE];

	char m1str [BUF_SIZE];
	char m2str [BUF_SIZE];
	char m3str [BUF_SIZE];
	char m4str [BUF_SIZE];

	char i1str [BUF_SIZE];
	char i2str [BUF_SIZE];
	char i3str [BUF_SIZE];
	char i4str [BUF_SIZE];
	
	int m1str_len;
	int m2str_len;
	int m3str_len;
	int m4str_len;
	
	int i1str_len;
	int i2str_len;
	int i3str_len;
	int i4str_len;
	
	int read_size;
	int rd_idx;
	
	int chk_idx = 0;


printf("argc:%d\n", argc); 
	if( argc >= 2) 
		chk_idx = atoi(argv[1]);
printf("chk_idx:%d\n", chk_idx); 
#if 0
exit(1);
#endif

	tuxputenv("WSNADDR=//10.55.2.140:14100");
	if (tpinit((TPINIT *) NULL) == -1) {
		(void) fprintf(stdout, "Tpinit failed[%s]\n", tpstrerror(tperrno));
		exit(1);
	}
	/* ----------------------------------------------------------------- */
	if((f_m1 = fopen( "../../data/iio_test/1.log", "rb" )) == NULL){
		fprintf(stdout,"f_m1 file open err\n");
		exit(0);
	}
	if((f_m2 = fopen( "../../data/iio_test/2.log", "rb" )) == NULL){
		fprintf(stdout,"f_m2 file open err\n");
		exit(0);
	}
	if((f_m3 = fopen( "../../data/iio_test/3.log", "rb" )) == NULL){
		fprintf(stdout,"f_m3 file open err\n");
		exit(0);
	}
	if((f_m4 = fopen( "../../data/iio_test/4.log", "rb" )) == NULL){
		fprintf(stdout,"f_m4 file open err\n");
		exit(0);
	}
	/* ----------------------------------------------------------------- */

	memset( m1str, 0, BUF_SIZE);
	rd_idx = 1;
	while( fgets((char *)m1str, BUF_SIZE, (FILE *)f_m1)) {;

fprintf( stdout, "[ READ index : %3d ]###########################################################\n", rd_idx);

		m1str_len = strlen(m1str);
		/* ----------------------------------------------------------*/
		memset( m2str, 0, BUF_SIZE);
    		m2str_len = fread((char *)m2str, 1, (int)MSG_SIZE, f_m2);
		/* ----------------------------------------------------------*/
		memset( m3str, 0, BUF_SIZE);
    		m3str_len = fread((char *)m3str, 1, (int)MSG_SIZE, f_m3);
		/* ----------------------------------------------------------*/
		memset( m4str, 0, BUF_SIZE);
/*
    		m4str_len = fread((char *)m4str, 1, (int)MSG_SIZE, f_m4);
*/
		m4str_len = fgets_l((char *)m4str, BUF_SIZE, (FILE *)f_m4);

fprintf( stdout, " 1. file read : m1(%d) -> m2*(%d), m3(%d) -> m4*(%d)\n",
m1str_len, m2str_len, m3str_len, m4str_len);
		if(chk_idx > 0) {
			if(rd_idx != chk_idx) {
				rd_idx++;
				continue;
			}
		}
fprintf( stderr, "[ index : %3d] #\n", rd_idx);
		/* ----------------------------------------------------------*/

fprintf( stdout, " 2. check : msg.1 -> msg.2\n");
		/* check step 01 --------------------------------------------*/
		memset( i1str, 0, BUF_SIZE);
		mk_msg1(i1str, m1str, &i1str_len, m1str_len);
fprintf( stdout, " ---- make message for IIO : (i1:%d)\n", i1str_len);

		sprintf( write_file_name,"%02d_iio1.txt", rd_idx);
		file_write( write_file_name, i1str, i1str_len);

#if 1
		memset( i2str, 0, BUF_SIZE);
		call_svc("S_A2E001", i1str, i2str, i1str_len, &i2str_len);
fprintf( stdout, " ---- after call IIO : (i2*:%d)\n", i2str_len);
fprintf( stdout, " ---- check difference between m2* .vs. i2* : (len:%d)\n", i2str_len);
		sprintf( write_file_name,"%02d_iio2.txt", rd_idx);
		file_write( write_file_name, i2str, i2str_len);

		/*
		file_write("2a.txt", m2str, m2str_len);
		file_write("2b.txt", i2str, i2str_len);
		*/

		chk_msg( m2str, i2str, i2str_len);
#endif
fprintf( stdout, " 3. check : msg.3 -> msg.4\n");
		/* check step 02 --------------------------------------------*/
		m3str_len = MSG_SIZE;
		memset( i3str, 0, BUF_SIZE);
		mk_msg2(i3str, m3str, &i3str_len, m3str_len);
fprintf( stdout, " ---- make message for IIO : (i3:%d)\n", i3str_len);

		sprintf( write_file_name,"%02d_iio3.txt", rd_idx);
		file_write( write_file_name, i3str, i3str_len);

		/*
		file_write("3a.txt", m3str, m3str_len);
		file_write("3b.txt", i3str, i3str_len);
		*/

		memset( i4str, 0, BUF_SIZE);
#if 1
		call_svc("S_E2A001", i3str, i4str, i3str_len, &i4str_len);
#endif
fprintf( stdout, " ---- after call IIO : (i4*:%d)\n", i4str_len);
fprintf( stdout, " ---- check difference between m4* .vs. i4* : (len:%d)\n", i4str_len);

		sprintf( write_file_name,"%02d_iio4.txt", rd_idx);
		file_write( write_file_name, i4str, i4str_len);

		/*
		file_write("4a.txt", m4str, m4str_len);
		file_write("4b.txt", i4str, i4str_len);
		*/

		chk_msg( m4str, i4str, m4str_len);
#if 0    
   		fprintf( stdout, "i4str_len: %d<\n", i4str_len);
#endif

		memset( m1str, 0, BUF_SIZE);
		rd_idx++;
	}

/*
   	fprintf( stdout, ">%s(%d)<\n", m2str, read_size);
   	fprintf( stdout, ">%s(%d)<\n", i2str, read_size);
*/

	if(f_m1) fclose(f_m1);	
	if(f_m2) fclose(f_m2);	
	if(f_m3) fclose(f_m3);	
	if(f_m4) fclose(f_m4);	
#if 0 

	sendlen = 1024;

	/* Allocate STRING buffers for the request and the reply */


#endif
	tpterm();
}
